var commJs = require('../../utils/common.js');
var appData = getApp().globalData;
// 在页面中定义激励视频广告
let videoAd = null

Page({
  data: {
    showPopup: false,

    isAd: 0,    // 0关闭 1页尾 2页头
    adBottom: "",
    adGift: "",

    wordJud: false,
    newsJud: true,
    scoreJud: false,
    moneyJud: false,
    kefuJud: true,
    shareJud: true,
    wordList: '',
    announ: '',
                
    type: '',
    value: '',
    focus: false,
    code: ''
  },
  onLoad(options) {
    const systemInfo = wx.getDeviceInfo();
    if (systemInfo.platform !== 'android' && systemInfo.platform !== 'ios') {
        this.setData({
            showPopup: true
        });
    }

    this.LoadSys();
  },

  LoadVideo(){
    videoAd = null;
    if (wx.createRewardedVideoAd) {
        videoAd = wx.createRewardedVideoAd({
            adUnitId: this.data.adGift
        })
        videoAd.onLoad(() => {
            console.log('激励视频广告onLoad')
        })
        videoAd.onError((err) => {
            console.error('激励视频广告加载失败', err, this.data.adGift)
            commJs.AlertTrue("视频加载失败提示","["+ err.errCode +"]"+ err.errMsg);
        })
        videoAd.onClose((res) => {
            console.log('激励视频广告onClose', res);
            if (res.isEnded){
                console.log('激励视频广告已看完');
                this.AddOrder();
            }else{
                console.log('激励视频广告未看完');
                commJs.AlertTrue("未看完提示","视频广告未看完不能获得奖励！");
            }
        })
    }
  },

  LoadSys(){
    console.log("加载【广告奖励参数】")
    var that = this;
    var paraArr = {
        action: "adSys",
        userID: 0,
        sign: "123456"
        }
    
    commJs.HttpPost(paraArr, "读取广告奖励参数").then(function (retArr) {
        console.log(retArr);
        if (! retArr.res){ commJs.ErrLog(retArr.note);return false; }
        let apiData = retArr.data.data;
        var dbArr = apiData.data;
        
        if (dbArr.XAS_isAd > 0){
            console.log("isAd开启"+ dbArr.XAS_isAd);
            that.setData({
                isAd: dbArr.XAS_isAd,
                adBottom: dbArr.XAS_adBottom,
                adGift: dbArr.XAS_adGift
            });
        }else{
            console.log("isAd关闭");
            that.setData({
                isAd: 0
            });
        }

        let wordJud = false;
        let newsJud = false;
        let scoreJud = false;
        let moneyJud = false;
        let kefuJud = false;
        let shareJud = false;
        if (dbArr.XAS_menuList.indexOf('|word|') != -1){
            wordJud = true;
        }
        if (dbArr.XAS_menuList.indexOf('|news|') != -1){
            newsJud = true;
        }
        if (dbArr.XAS_menuList.indexOf('|score|') != -1){
            scoreJud = true;
        }
        if (dbArr.XAS_menuList.indexOf('|money|') != -1){
            moneyJud = true;
        }
        if (dbArr.XAS_menuList.indexOf('|kefu|') != -1){
            kefuJud = true;
        }
        if (dbArr.XAS_menuList.indexOf('|share|') != -1){
            shareJud = true;
        }
        that.setData({
            wordJud: wordJud,
            newsJud: newsJud,
            scoreJud: scoreJud,
            moneyJud: moneyJud,
            kefuJud: kefuJud,
            shareJud: shareJud,
            wordList: dbArr.XAS_wordList,
            announ: dbArr.XAS_announ
        })
        that.LoadVideo();
    })
  },

  AddOrder(){
    var paraArr = {
        action: "adOrder",
        userID: 0,
        key: appData.sysKey,
        unionid: appData.user.unionid,
        openid: appData.user.openid,
        type: this.data.type,
        value: this.data.value,
        sign: "123456"
        }
    
    var that = this;
    commJs.HttpPost(paraArr, "添加广告奖励订单").then(function (retArr) {
        // console.log(retArr);
        if (! retArr.res){ commJs.ErrLog(retArr.note);return false; }
        let apiData = retArr.data.data;
        var dbArr = apiData.data;
        
        wx.showModal({
            title: "看完领取奖励",
            content: apiData.note,
            showCancel: false,
            confirmText: "复制内容",
            success: function(res) {
                if (res.confirm) {
                    console.log('用户点击确定');

                    wx.setClipboardData({
                        data: apiData.note,
                        success: function() {
                            wx.showToast({
                                title: '复制成功',
                                duration: 3000
                            })
                        }
                    })
                } else if (res.cancel) {
                    console.log('用户点击取消');
                }
                return true;
            },
            fail: function(res) {
                return false;
            }
        });
    })
  },

  showAd(e){
    var type_ = commJs.StrDef(e.currentTarget.dataset.type);

    if (type_ == "word"){
        if (this.data.value == ""){
            commJs.AlertErr('请先输入关键词');
            this.setData({ focus: true });
            return false;
        }
        if (this.data.wordList.indexOf('|'+ this.data.value +'|') == -1){
            commJs.AlertNone('关键词“'+ this.data.value +'”不存在，请重新输入或联系管理员');
            this.setData({ focus: true });
            return false;
        }
    }

    this.setData({ type: type_ });

    if (this.data.isAd > 0){
        // 广告开启时，用户触发广告后，显示激励视频广告
        if (videoAd) {
            wx.showLoading({ title: '视频加载中' })
            videoAd.show().then(() => {
                // 广告显示成功
                wx.hideLoading();
            }).catch(() => {
                // 广告显示失败，重试
                videoAd.load()
                    .then(() => videoAd.show())
                    .catch(err => {
                        wx.hideLoading();
                        console.error('视频广告显示失败', err)
                    })
            })
        }
    }else{
        // 广告关闭时直接领取奖励
        this.AddOrder();
    }
  },

  SearchInput(e){
    var val = e.detail.value;
    
    this.setData({ value: val });
  },

  // 广告事件
  adLoad(e) {
    console.log('原生模板广告加载成功', e);
  },
  adError(err) {
    console.error('原生模板广告加载失败', err);
  },
  adClose(e) {
    console.log('原生模板广告关闭', e);
  },
  adHide(e) {
    console.log('原生模板广告隐藏', e);
  },

  
  // 页面相关事件处理函数--监听用户下拉动作
  onPullDownRefresh() {
    console.log("下拉刷新");
    this.onReady();
    wx.stopPullDownRefresh(); // 停止下拉刷新动画
  },

  // 用户点击右上角分享
  onShareAppMessage() {
    console.log("点击右上角分享");
  }

})
